package outoforder.leetcode;

import java.util.ArrayDeque;
import java.util.LinkedList;

/**
 * @author shhjiang_001
 * @create 2020-07-04 14:41
 */
public class Solution111 {
    public int minDepth(TreeNode root) {
        if (root==null) return 0;
        ArrayDeque<TreeNode> deque = new ArrayDeque<>();
        deque.add(root);
        int depth = 1;
        while (!deque.isEmpty()){
            int sz = deque.size();
            for (int i = 0; i < sz; i++) {
                TreeNode cur = deque.pop();
                if (cur.left == null && cur.right==null){
                    return depth;
                }
                if (cur.left != null){
                    deque.add(cur.left);
                }
                if (cur.right != null){
                    deque.add(cur.right);
                }
            }
            depth++;
        }
        return depth;
    }
}
